package org.icress.fusion.moc

import org.apache.shiro.subject.Subject
import org.apache.shiro.SecurityUtils

import org.icress.fusion.shiro.User

class TcController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
	
	def tcService

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 100, 100)
        render view : '/moc/tc/list', model : [tcInstanceList: tcService.list(params), tcInstanceTotal: tcService.count()]
    }
	
    def create = {
        def tcInstance = new Tc()
        tcInstance.properties = params
        
        def tbInstance = Tb.get(params.tb_id)
        tcInstance.tb = tbInstance
        
        Td.listOrderByTd001().each {
            tcInstance.addToTgs(new Tg(td : it, tg001 : 0, tg002 : it.td002))  
        }
                
        render view : '/moc/tc/create', model : [tcInstance: tcInstance]
    }

    def save = {
        
        def tcInstance = new Tc(params)
        if(tcInstance.validate() && !tcInstance.hasErrors() && tcService.save(tcInstance, params, true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'moc.tc.label', default: 'Tc'), tcInstance.id])}"
            redirect(controller : "tc", action: "show", id: tcInstance.id)
        }else {
            render(view: "/moc/tc/create", model: [tcInstance: tcInstance])
        }
    }

    def show = {
        def tcInstance = tcService.get(params.id)
        if (!tcInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'moc.tc.label', default: 'Tc'), params.id])}"
            redirect(action: "list")
        }else {
            render view : '/moc/tc/show', model : [tcInstance: tcInstance]
        }
    }

    def edit = {
        def tcInstance = tcService.get(params.id)
		
        if (!tcInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'moc.tc.label', default: 'Tc'), params.id])}"
            redirect(action: "list")
        } else {
			Subject currentUser = SecurityUtils.getSubject();
			if(currentUser.hasRole('admin') || currentUser.hasRole('manager') || currentUser.getPrincipal().equals(tcInstance.owner.username) ){
				render view : '/moc/tc/edit', model : [tcInstance: tcInstance]
			}else{
	            render view : '/unauthorized'
			}
        }
    }

    def update = {
        def tcInstance = tcService.get(params.id)
        
        if (!tcInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'moc.tc.label', default: 'Tc'), params.id])}"
            redirect(action: "list")
        }
        
        if (params.version) {
            def version = params.version.toLong()
            if (tcInstance.version > version) {
                
                tcInstance.errors.rejectValue("version", "default.optimistic.locking.failure", 
                    [message(code: 'moc.tc.label', default: 'Tc')] as Object[], "Another user has updated this Tc while you were editing")
                render(view: "/moc/tc/edit", model: [tcInstance: tcInstance])
                return
            }
        }
        //delete details
        
        
        if (tcService.update(tcInstance, params, true)) {
            flash.message = "${message(code: 'default.updated.message', args: [message(code: 'moc.tc.label', default: 'Tc'), tcInstance.id])}"
            redirect(controller : "tc", action: "show", id: tcInstance.id)
        }else {
            render(view: "/moc/tc/edit", model: [tcInstance: tcInstance])
        }
    }

    def delete = {
        def tcInstance = tcService.get(params.id)
        if (!tcInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'moc.tc.label', default: 'Tc'), params.id])}"
            redirect(action: "list")
        }
        try {
            tcService.delete(tcInstance, true)
            flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'moc.tc.label', default: 'Tc'), params.id])}"
            redirect(action: "list")
        }catch (org.springframework.dao.DataIntegrityViolationException e) {
            flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'moc.tc.label', default: 'Tc'), params.id])}"
            redirect(action: "show", id: params.id)
        }
    }
	
	def related_tgs = {
		def tcInstance = tcService.get(params.id)
		if (!tcInstance) {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'moc.tc.label', default: 'Tc'), params.id])}"
			redirect(action: "list")
		}else {
			params.max = params.max ? params.int('max') : 100
			def tgInstanceList = Tg.findAllByTc(tcInstance, [offset:params.offset, max : params.max, sort : params.sort, order : params.order ])
			render view : '/moc/tc/related_tgs', model : [tgInstanceList: tgInstanceList, tgInstanceListTotal: tcInstance.tgs.size()]
		}
	}
	
	def related_ths = {
		def tcInstance = tcService.get(params.id)
		if (!tcInstance) {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'moc.tc.label', default: 'Tc'), params.id])}"
			redirect(action: "list")
		}else {
			params.max = params.max ? params.int('max') : 100
			def thInstanceList = Th.findAllByTc(tcInstance, [offset:params.offset, max : params.max, sort : params.sort, order : params.order ])
			render view : '/moc/tc/related_ths', model : [thInstanceList: thInstanceList, thInstanceListTotal: tcInstance.ths.size()]
		}
	}
}
